<?php
class Secure_area extends CI_Controller 
{
	/*
	Controllers that are considered secure extend Secure_area, optionally a $module_id can
	be set to also check if a user can access a particular module in the system.
	*/
	function __construct($module_id=null)
	{
		parent::__construct();	
		$this->load->model('Employee');
		if(!$this->Employee->is_logged_in())
		{
			redirect('login');
		}
		
		if(!$this->Employee->has_permission($module_id,$this->Employee->get_logged_in_employee_info()->person_id))
		{
			redirect('no_access/'.$module_id);
		}
		
		//load up global data
		$logged_in_employee_info=$this->Employee->get_logged_in_employee_info();
		$data['allowed_modules']=$this->Module->get_allowed_modules($logged_in_employee_info->person_id);
		$data['user_info']=$logged_in_employee_info;
		$this->load->vars($data);
		
		//swtich store
		if ($this->input->post('swich_to_store') != '' and $this->input->post('swich_to_store') != $this->session->userdata('store_id')){
			$this->session->set_userdata('store_id', $this->input->post('swich_to_store'));	
		}
		$this->reload_store_config();
	}
	
	function reload_store_config(){
		//reload store config
		$CI =& get_instance();
		$this->load->model('Store');
		$query = $this->Store->get_info($this->session->userdata('store_id'));
		foreach ($query as $key => $value)
		{
			$CI->config->set_item($key,$value);
		}		
		
		if ($CI->config->item('language'))
		{
			$CI->lang->switch_to($CI->config->item('language'));
		}
		
		if ($CI->config->item('timezone'))
		{
			date_default_timezone_set($CI->config->item('timezone'));
		}
		else
		{
			date_default_timezone_set('America/New_York');
		}
	}
}
?>